% Copyright 2003--2007 by Till Tantau
% Copyright 2010--2013,2015 by Vedran Mileti\'c, Joseph Wright
% Copyright 2017,2018 by Louis Stuart, Joseph Wright
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/licenses/LICENSE for more details.

% Many of these commans allow an overlay spec either before or after
% their argument (though not in both positions), hence the repetition
% of "\@ifnextchar<".

%
%
% Basic overlay commands
%
%

% This variant based on \@ifnextchar does not skip spaces
% (So like amsmath's \new@ifnextchar). It is used where beamer allows
% an overlay at the end of a command, and would thus otherwise result in
% space gobbling.
\long\def\beamer@ifnextcharospec#1#2{%
  \def\reserved@a{#1}%
  \def\reserved@b{#2}%
  \futurelet\@let@token\beamer@ifnch}
\def\beamer@ifnch{%
  \ifx\@let@token<%
    \let\reserved@c\reserved@a%
  \else%
    \let\reserved@c\reserved@b%
  \fi%
  \reserved@c}

%
% \only
%
\newrobustcmd*\only{\@ifnextchar<{\beamer@only}{\beamer@@only}}
\long\def\beamer@only<#1>#2{%
  \gdef\beamer@doifnotinframe{}\def\beamer@doifinframe{#2}%
  \beamer@masterdecode{#1}\beamer@donow}
\long\def\beamer@@only#1{\beamer@ifnextcharospec{\beamer@@@only{#1}}{#1}}
\long\def\beamer@@@only#1<#2>{%
  \gdef\beamer@doifnotinframe{}\def\beamer@doifinframe{#1}%
  \beamer@masterdecode{#2}\beamer@donow}

%
% \alt and \altenv
%
\newrobustcmd*\alt{\@ifnextchar<{\beamer@alt}{\beamer@alttwo}}
\long\def\beamer@alttwo#1#2{\beamer@ifnextcharospec{\beamer@altget{#1}{#2}}{#1}}
\long\def\beamer@altget#1#2<#3>{%
  \gdef\beamer@doifnotinframe{#2}\def\beamer@doifinframe{#1}%
  \beamer@masterdecode{#3}\beamer@donow}
\long\def\beamer@alt<#1>#2#3{%
  \gdef\beamer@doifnotinframe{#3}\def\beamer@doifinframe{#2}%
  \beamer@masterdecode{#1}\beamer@donow}


\newrobustcmd*\altenv{\@ifnextchar<{\beamer@altenv}{\beamer@@@altenv}}
\def\beamer@altenv<#1>{\beamer@@altenv{<#1>}}
\long\def\beamer@@@altenv#1#2#3#4{\beamer@ifnextcharospec{\beamer@altenvsort{#1}{#2}{#3}{#4}}{\beamer@@altenv{}{#1}{#2}{#3}{#4}}}
\long\def\beamer@altenvsort#1#2#3#4<#5>{\beamer@@altenv{<#5>}{#1}{#2}{#3}{#4}}
\long\def\beamer@@altenv#1#2#3#4#5{\alt#1{\long\def\beamer@eoenv{#3}#2}{\long\def\beamer@eoenv{#5}#4}}
\protected\def\endaltenv{\beamer@eoenv}

%
% \action and actionenv
%
\def\beamer@plus{+}

\newrobustcmd*\action{\@ifnextchar<{\beamer@parsedaction}{\expandafter\beamer@parsedaction\beamer@defaultospec}}
\long\def\beamer@parsedaction<#1>#2{%
  \gdef\beamer@doifnotinframe{}%
  \def\beamer@doifinframe{}%
  \beamer@masterdecode{#1}%
  \ifx\beamer@decodefound\beamer@plus
    \long\def\beamer@todo{#2}%
  \else
    \expandafter\long\expandafter\def\expandafter\beamer@todo
    \expandafter{\expandafter\uncover\expandafter<\expandafter a\expandafter l\expandafter l\expandafter:\beamer@decodefound>{#2}}%
  \fi
  \expandafter\beamer@processactions\beamer@decaction{}%
  \beamer@todo}

\def\beamer@processactions#1{%
  \ifblank{#1}{}{%
    \expandafter\beamer@@processactions\expandafter{\beamer@todo}#1}}
\long\def\beamer@@processactions#1#2<#3>{%
  \expandafter\long\expandafter\def\expandafter\beamer@todo\expandafter{%
    \csname#2\endcsname<all:#3>{#1}}%
  \beamer@processactions}

\newrobustcmd*\actionenv{\beamer@ifnextcharospec{\beamer@parsedactionenv}{\expandafter\beamer@parsedactionenv\beamer@defaultospec}}
\def\beamer@parsedactionenv<#1>{%
  \gdef\beamer@doifnotinframe{}%
  \def\beamer@doifinframe{}%
  \beamer@masterdecode{#1}%
  \ifx\beamer@decodefound\beamer@plus
    \def\beamer@todobegin{}%
    \def\beamer@todoend{}%
  \else
    \edef\beamer@todobegin{\noexpand\begin{uncoverenv}<all:\beamer@decodefound>}%
    \def\beamer@todoend{\end{uncoverenv}}%
  \fi
  \expandafter\beamer@processactionsbegin\beamer@decaction{}%
  \expandafter\beamer@processactionsend\beamer@decaction{}%
  \beamer@todobegin}
\protected\def\endactionenv{\beamer@todoend}

\def\beamer@processactionsbegin#1{%
  \ifblank{#1}{}{%
    \expandafter\beamer@@processactionsbegin\expandafter{\beamer@todobegin}#1}}
\long\def\beamer@@processactionsbegin#1#2<#3>{%
  \def\beamer@todobegin{\begin{#2env}<all:#3>#1}%
  \beamer@processactionsbegin}

\def\beamer@processactionsend#1{%
  \ifblank{#1}{}{%
    \expandafter\beamer@@processactionsend\expandafter{\beamer@todoend}#1}}
\long\def\beamer@@processactionsend#1#2<#3>{%
  \def\beamer@todoend{#1\end{#2env}}%
  \beamer@processactionsend}

\def\beamer@defaultospec{<*>}
\def\beamer@emptyospec{<*>}

%
% \temporal
%
\protected\long\def\temporal<#1>#2#3#4{% past, present, future
  \gdef\beamer@doifnotinframe{\beamer@picked}\def\beamer@doifinframe{#3}%
  {\beamer@masterdecode{#1}%
  \ifbeamer@localanotherslide\gdef\beamer@picked{#2}\else\gdef\beamer@picked{#4}\fi}\beamer@donow}

%
% \beameroriginal
%
\def\beameroriginal#1{\csname @orig\string#1\endcsname}

\newcount\beamer@argscount

%
% newenvironment extension
%
\let\beamer@orignewenvironment=\newenvironment

\def\newenvironment{\@ifnextchar<{\beamer@newenv}{\beamer@orignewenvironment}}
\def\beamer@newenv<>{\@star@or@long\beamer@new@environment}
\def\beamer@new@environment#1{\@ifnextchar[{\beamer@@newenv{#1}}{\beamer@newenvnoopt{#1}{0}}}
\def\beamer@@newenv#1[#2]{\@ifnextchar[{\beamer@newenvopt{#1}{#2}}{\beamer@newenvnoopt{#1}{#2}}}
% The beamer syntax for \newenvironment<> follows the pattern for \newcommand<>
% and allows the overlay spec anywhere: the same code path is therefore used for
% both.
\long\def\beamer@newenvnoopt#1#2#3#4{%
  \expandafter\newcommand\expandafter<\expandafter>\csname#1\endcsname[#2]{#3}%
  \expandafter\long\expandafter\def\csname end#1\endcsname{#4}%
}
\long\def\beamer@newenvopt#1#2[#3]#4#5{%
  \expandafter\newcommand\expandafter<\expandafter>\csname#1\endcsname[#2][#3]{#4}%
  \expandafter\long\expandafter\def\csname end#1\endcsname{#5}%
}

\let\beamer@origrenewenvironment=\renewenvironment

\def\renewenvironment{\@ifnextchar<{\beamer@renewenv}{\beamer@origrenewenvironment}}
\def\beamer@renewenv<>#1{%
  \ifcsdef{original#1}%
    {}%
    {%
      \csletcs{original#1}{#1}%
      \csletcs{endoriginal#1}{end#1}%
    }%
  \csundef{#1}%
  \csundef{beamerx@\expandafter\string\csname#1\endcsname}%
  \newenvironment<>{#1}}

%
% newcommand extension
%
\let\beamer@orignewcommand=\newcommand

\def\newcommand{\@ifnextchar<{\beamer@newcom}{\beamer@orignewcommand}}
\def\beamer@newcom<>{\@star@or@long\beamer@new@command}
\def\beamer@new@command#1{\@ifnextchar[{\beamer@@newcom{#1}}{\beamer@newcomnoopt{#1}{0}}}
\def\beamer@@newcom#1[#2]{\@ifnextchar[{\beamer@newcomopt{#1}{#2}}{\beamer@newcomnoopt{#1}{#2}}}
\long\def\beamer@newcomnoopt#1#2#3{%
  \ifnum#2=0\relax
    \protected\edef#1%
      {\noexpand\beamer@sortzero\expandafter\noexpand\csname beamerx@\string#1\endcsname}%
  \else
    \protected\edef#1%
      {\noexpand\beamer@sort\expandafter\noexpand\csname beamerx@\string#1\endcsname{#2}}%
  \fi
  \beamer@argscount=#2\relax
  \advance\beamer@argscount by 1\relax
  \ifx\l@ngrel@x\relax
    \expandafter\expandafter\expandafter\newcommand
      \expandafter\expandafter\expandafter*%
  \else
    \expandafter\expandafter\expandafter\newcommand
  \fi
    \csname beamerx@\string#1\endcsname[\beamer@argscount]{#3}%
}
\long\def\beamer@newcomopt#1#2[#3]#4{%
  \protected\expandafter\def\expandafter#1\expandafter
    {\expandafter\beamer@presort\expandafter{\csname beamerx@\string#1\endcsname}{#2}{#3}}%
  \beamer@argscount=#2\relax
  \advance\beamer@argscount by 1\relax
  \ifx\l@ngrel@x\relax
    \expandafter\expandafter\expandafter\newcommand
      \expandafter\expandafter\expandafter*%
  \else
    \expandafter\expandafter\expandafter\newcommand
  \fi
    \csname beamerx@\string#1\endcsname[\beamer@argscount]{#4}%
}

\let\beamer@origrenewcommand=\renewcommand

\def\renewcommand{\@ifnextchar<{\beamer@renewcom}{\beamer@origrenewcommand}}
\def\beamer@renewcom<>#1{%
  \ifcsundef{@orig\string#1}%
    {\cslet{@orig\string#1}#1}%
    {}%
  \csundef{beamerx@\string#1}%
  \newcommand<>#1}

% The class allows overlays specs at any position in a command.
% To handle that, beamer first collects up material potentially
% including overlay info before passing to the 'real' definition.
\long\def\beamer@presort#1#2#3{%
  \def\beamer@todo{#1}%
  \def\beamer@ospec{}%
  \beamer@argscount=#2\relax
  \beamer@prechecka{#3}}
\long\def\beamer@prechecka#1{\@ifnextchar<{\beamer@preget{#1}}{\beamer@precheckb{#1}}}
\long\def\beamer@preget#1<#2>{\def\beamer@ospec{<#2>}\beamer@precheckb{#1}}
\long\def\beamer@precheckb#1{\@ifnextchar[{\beamer@pregetb}{\beamer@pregetb[{#1}]}}
\long\def\beamer@pregetb[#1]{%
  \expandafter\def\expandafter\beamer@todo\expandafter{\beamer@todo{#1}}%
  \advance\beamer@argscount by-1\relax
  \beamer@parseargs
}

\def\beamer@sortzero#1{\beamer@ifnextcharospec{\beamer@sortzeroread{#1}}{#1{}}}
\def\beamer@sortzeroread#1<#2>{#1{<#2>}}

\def\beamer@sort#1#2{%
  \def\beamer@todo{#1}%
  \def\beamer@ospec{}%
  \beamer@argscount=#2\relax
  \beamer@parseargs}

\def\beamer@parseargs{%
  \ifnum\beamer@argscount=0\relax
    \let\next=\beamer@finalargscheck
  \else
    \let\next=\beamer@lookforarg
  \fi
  \next}
\def\beamer@lookforarg{%
  \@ifnextchar<\beamer@foundspec\beamer@readarg}
\def\beamer@foundspec<#1>{%
  \def\beamer@ospec{<#1>}%
  \beamer@lookforarg}
\long\def\beamer@readarg#1{%
  \expandafter\long\expandafter\def\expandafter\beamer@todo\expandafter{\beamer@todo{#1}}%
  \advance\beamer@argscount by-1\relax
  \beamer@parseargs
}

\def\beamer@finalargscheck{\beamer@ifnextcharospec\beamer@finalspec\beamer@finalnospec}
\def\beamer@finalspec<#1>{\def\beamer@ospec{<#1>}\beamer@finalnospec}
\def\beamer@finalnospec{%
  \expandafter\beamer@todo\expandafter{\beamer@ospec}}

\mode
<presentation>

% commands for smuggling skips past

\newskip\beamer@lastskipcover
\def\beamer@smuggle#1{%
  {}%
  \beamer@lastskipcover=\lastskip
  \edef\beamer@lastskiptext{\the\lastskip}%
  \ifx\beamer@lastskiptext\beamer@zeropt
  \else
    \ifvmode\unskip\fi
    \ifhmode\unskip\fi
  \fi
  #1%
  \ifx\beamer@lastskiptext\beamer@zeropt
  \else
    \ifvmode\vskip\beamer@lastskipcover\fi
    \ifhmode\hskip\beamer@lastskipcover\fi
  \fi
}

%
% Uncovering management
%
\def\beamer@actions#1#2{%
  \gdef\beamer@do{#1%
    \expandafter\gdef\csname beamer@doafter%
    \the\beamer@coveringdepth\endcsname{#2}}}

\def\beamer@startcovered{%
  \beamer@smuggle{%
  \global\advance\beamer@coveringdepth by 1\relax%
  \beamer@actions{\pgfsys@begininvisible\aftergroup\beamer@@spacingcover}{%
     \pgfsys@endinvisible\aftergroup\beamer@@spacingcover%
     }%
  \def\opaqueness<##1>##2{%
    \only<##1>{%
      \beamer@actions{%
        \expandafter\xdef\csname beamer@oldcolorhook%
        \the\beamer@coveringdepth\endcsname{\beamer@colorhook}%
        \expandafter\xdef\csname beamer@oldpgfextension%
        \the\beamer@coveringdepth\endcsname{\beamer@pgfextension}%
        {\globalcolorstrue\colorlet{beamer@freeze\the\beamer@coveringdepth}{bg}}%
        \xdef\beamer@colorhook{!##2!beamer@freeze%
          \the\beamer@coveringdepth\beamer@colorhook}%
        \gdef\beamer@pgfextension{!##2opaque}%
        \color{.}%
      }%
      {%
        \xdef\beamer@colorhook{\csname beamer@oldcolorhook%
          \the\beamer@coveringdepth\endcsname}%
        \xdef\beamer@pgfextension{\csname beamer@oldpgfextension%
          \the\beamer@coveringdepth\endcsname}%
        \color{.}%
      }}}%
  \ifnum\beamer@slideinframe<\beamer@minimum %ok, at beginning
    \begingroup
      \beamer@saveanother
      \advance\beamer@minimum by-\beamer@slideinframe
      \beamer@slideinframe=\beamer@minimum
      \beamer@uncoverbeforeactions
      \beamer@restoreanother
    \endgroup
  \else
    \begingroup
      \beamer@saveanother
      \advance\beamer@slideinframe by-\beamer@minimum
      \beamer@uncoverafteractions
      \beamer@restoreanother
    \endgroup
  \fi
  \beamer@do
  }%
}
\def\beamer@endcovered{%
  \beamer@smuggle{%
  \csname beamer@doafter\the\beamer@coveringdepth\endcsname
  \global\advance\beamer@coveringdepth by -1\relax
  }%
}%

\define@key{beamer@mixin}{still covered}{\def\beamer@uncoverbeforeactions{\ignorespaces#1}}
\define@key{beamer@mixin}{again covered}{\def\beamer@uncoverafteractions{\ignorespaces#1}}

\define@key{beamer@mixin}{invisible}[]{%
  \def\beamer@uncoverbeforeactions{\ignorespaces}%
  \def\beamer@uncoverafteractions{\ignorespaces}}

\define@key{beamer@mixin}{transparent}[15]{%
  \def\beamer@uncoverbeforeactions{\ignorespaces\opaqueness<1->{#1}}%
  \def\beamer@uncoverafteractions{\ignorespaces\opaqueness<1->{#1}}}

\define@key{beamer@mixin}{dynamic}[]{%
  \def\beamer@uncoverbeforeactions{\ignorespaces\opaqueness<1-2>{10}\opaqueness<3->{5}}%
  \def\beamer@uncoverafteractions{\ignorespaces\opaqueness<1-2>{10}\opaqueness<3->{5}}}

\define@key{beamer@mixin}{highly dynamic}[]{%
  \def\beamer@uncoverbeforeactions{\ignorespaces\opaqueness<1>{15}\opaqueness<2>{10}\opaqueness<3>{5}\opaqueness<4->{2}}%
  \def\beamer@uncoverafteractions{\ignorespaces\opaqueness<1>{15}\opaqueness<2>{10}\opaqueness<3>{5}\opaqueness<4->{2}}}

\newrobustcmd*\setbeamercovered[1]{%
  \setkeys{beamer@mixin}{invisible}%
  \setkeys{beamer@mixin}{#1}}

\setbeamercovered{invisible}

\newcolormixin\beamer@colorhook

\def\pgfalternateextension{\colorcurrentmixin\beamer@pgfextension}

\def\beamer@colorhook{}
\def\beamer@pgfextension{}

\beamer@coveringdepth=0\relax

\mode
<article>
{
  \newrobustcmd*\setbeamercovered[1]{}
}

%
% Pausing
%

\def\beamer@endpause{}

\newenvironment{beamer@framepauses}%internal
{%
  \setcounter{beamerpauses}{1}%
  \gdef\beamer@endpause{}%
}
{\beamer@endpause}

\newrobustcmd{\beamer@@pause}[1][]{%
  \ifblank{#1}%
    {\stepcounter{beamerpauses}}%
    {\setcounter{beamerpauses}{#1}}%
  \onslide<\value{beamerpauses}->\relax%
}

\mode
<all>
{
  \AtBeginDocument
  {
    \let\beamer@musixpause=\pause
    \let\pause=\beamer@@pause

    \let\beamer@music=\music
    \def\music{\let\beamerpause=\pause\let\pause\beamer@musixpause\beamer@music}
  }
}

\mode
<all>
{
  \newrobustcmd*\onslide{\@ifstar{\only}{\@ifnextchar+{\expandafter\beamer@visibleonslide\@gobble}{\beamer@mainonslide}}}

  \newcommand<>{\beamer@visibleonslide}{%
    \@ifnextchar\bgroup
    {\visible#1}%
    {\beamer@noargsvisibleonslide#1}}

  \newcommand<>{\beamer@mainonslide}{%
    \@ifnextchar\bgroup%
    {\uncover#1}%
    {\beamer@noargsonslide#1}%
  }
}

\newcommand<>{\beamer@noargsonslide}{%
  \beamer@endpause%
  \alt#1%
  {\beamer@spacingcover\gdef\beamer@endpause{\beamer@spacingcover}}%
  {\beamer@startcovered\gdef\beamer@endpause{\beamer@endcovered}}%
}

\newcommand<>{\beamer@noargsvisibleonslide}{%
  \beamer@endpause%
  \alt#1%
  {\beamer@spacingcover\gdef\beamer@endpause{\beamer@spacingcover}}%
  {\beamer@begininvisible\gdef\beamer@endpause{\beamer@endinvisible}}%
}

\mode
<article>
{
  \newrobustcmd\beamer@@pause[1][]{}
  \newcommand<>{\beamer@noargsonslide}{}
  \newcommand<>{\beamer@noargsvisibleonslide}{}
}

%
% Make item overlays
%

\let\beamer@origitem=\item
\def\beamer@callorigitem{\@ifnextchar[\beamer@@callorigitem{\beamer@origitem\kern0pt\ignorespaces}}
\def\beamer@@callorigitem[#1]{\beamer@origitem[{#1}]\kern0pt\ignorespaces}

\def\item{%
  \@ifnextchar\relax\beamer@origitem{%
  \ifhmode
    \unskip\unskip\par
  \fi
  \beamer@closeitem\gdef\beamer@closeitem{}%
  \@ifnextchar<{\beamer@parseitem}{\beamer@itemcheckopt}}}
\def\beamer@itemcheckopt{\@ifnextchar[{\beamer@itemcheckopta}{%
    \ifx\beamer@defaultospec\beamer@emptyospec
      \let\next=\beamer@callorigitem
    \else
      \let\next=\beamer@defaultparse
    \fi
    \next
  }}
\def\beamer@defaultparse{\expandafter\beamer@parseitem\beamer@defaultospec}
\long\def\beamer@itemcheckopta[#1]{\@ifnextchar<{\beamer@itemreverse[{#1}]}{%
    \ifx\beamer@defaultospec\beamer@emptyospec
      \let\next=\beamer@callorigitem
    \else
      \let\next=\beamer@defaultparse
    \fi
    \next[{#1}]}}
\long\def\beamer@itemreverse[#1]<#2>{\beamer@parseitem<#2>[{#1}]}

\def\beamer@parseitem<#1>{%
  \gdef\beamer@closeitem{%
    %% Handle empty item as LaTeX would do
    \if@inlabel\indent\par\fi
    \end{actionenv}%
    %% Local change inside actionenv should survive, and should be
    %% always false
    \@noparitemfalse}%
  \begin{actionenv}<#1>\beamer@callorigitem}

\newrobustcmd*\beamerdefaultoverlayspecification[1]{%
  \ifblank{#1}%
  {%
    \def\beamer@gdefaultospec{<*>}%
    \def\beamer@defaultospec{<*>}}%
  {%
    \def\beamer@gdefaultospec{#1}%
    \def\beamer@defaultospec{#1}%
  }%
}
\beamerdefaultoverlayspecification{}

\newcount\beamer@trivlistdepth
\beamer@trivlistdepth=0\relax

\let\beamer@orig@trivlist=\@trivlist
\def\@trivlist{%
  \global\advance\beamer@trivlistdepth by1\relax
  \expandafter\global\expandafter\let\csname beamer@closeitem\the\beamer@trivlistdepth\endcsname=\beamer@closeitem
  \gdef\beamer@closeitem{}%
  \beamer@orig@trivlist
  }
\let\beamer@origendtrivlist=\endtrivlist
\def\endtrivlist{%
  \beamer@closeitem
  \expandafter\global\expandafter\let\expandafter\beamer@closeitem\expandafter=\csname
  beamer@closeitem\the\beamer@trivlistdepth\endcsname
  \global\advance\beamer@trivlistdepth by-1\relax
  \beamer@origendtrivlist}

\def\beamer@closeitem{}

\let\beamer@list=\list
\let\beamer@@trivlist=\@trivlist

\mode
<article>
{
  \AtBeginDocument{%
    \let\beamer@origitem=\item
    \def\item{\@ifnextchar<\beamer@@item\beamer@item}
    \def\beamer@@item<#1>{\beamer@item}
    \def\beamer@item{\@ifnextchar[\beamer@@@item\beamer@origitem}
    \def\beamer@@@item[#1]{\@ifnextchar<{\beamer@@@@item[{#1}]}{\beamer@origitem[{#1}]}}
    \def\beamer@@@@item[#1]<#2>{\beamer@origitem[{#1}]}
    \newrobustcmd*\beamerdefaultoverlayspecification[1]{}
  }
}

%
% Overlay commands
%

\mode
<all>
{
  \newcommand\beamer@gobbleoptional[2][]{}
  \newcommand\beamer@gobbleoptionalinsp[2][]{\ignorespaces}
}

\def\beamer@@spacingcover{\beamer@smuggle{\pgfsys@begininvisible\pgfsys@endinvisible}}
\def\beamer@spacingcover{\beamer@@spacingcover\aftergroup\beamer@@spacingcover}

\def\beamer@begininvisible{\beamer@smuggle{\pgfsys@begininvisible\aftergroup\beamer@@spacingcover}}
\def\beamer@endinvisible{\beamer@smuggle{\pgfsys@endinvisible\aftergroup\beamer@@spacingcover}}

\long\def\beamer@fakeinvisible#1{\beamer@spacingcover#1\beamer@spacingcover}
\long\def\beamer@makecovered#1{\beamer@startcovered#1\beamer@endcovered}
\long\def\beamer@reallymakeinvisible#1{\beamer@begininvisible#1\beamer@endinvisible}

\newrobustcmd*{\uncover}{\alt{\beamer@fakeinvisible}{\beamer@makecovered}}
\newrobustcmd*{\visible}{\alt{\beamer@fakeinvisible}{\beamer@reallymakeinvisible}}
\newrobustcmd*{\invisible}{\alt{\beamer@reallymakeinvisible}{\beamer@fakeinvisible}}
\newenvironment{visibleenv}{\begin{altenv}{\beamer@spacingcover}{\beamer@spacingcover}{\beamer@begininvisible}{\beamer@endinvisible}}{\end{altenv}}
\newenvironment{invisibleenv}{\begin{altenv}{\beamer@begininvisible}{\beamer@endinvisible}{\beamer@spacingcover}{\beamer@spacingcover}}{\end{altenv}}
\newenvironment{uncoverenv}{\begin{altenv}{\beamer@spacingcover}{\beamer@spacingcover}{\beamer@startcovered}{\beamer@endcovered}}{\end{altenv}}
\newenvironment{onlyenv}{\begin{altenv}{}{}{\begingroup\setbox0=\vbox\bgroup}{\egroup\endgroup}}{\end{altenv}}
\renewcommand<>{\color}{\alt#1{\beameroriginal{\color}}{\beamer@gobbleoptionalinsp}}

\mode
<article>
{
  \newrobustcmd*{\uncover}{\alt{\@firstofone}{\@gobble}}
  \newrobustcmd*{\visible}{\alt{\@firstofone}{\@gobble}}
  \newrobustcmd*{\invisible}{\alt{\@gobble}{\@firstofone}}
  \newenvironment{onlyenv}{\begin{altenv}{}{}{\begingroup\setbox0=\vbox\bgroup}{\egroup\endgroup}}{\end{altenv}}
  \newenvironment{uncoverenv}{\begin{altenv}{}{}{\begingroup\setbox0=\vbox\bgroup}{\egroup\endgroup}}{\end{altenv}}
  \newenvironment{visibleenv}{\begin{altenv}{}{}{\begingroup\setbox0=\vbox\bgroup}{\egroup\endgroup}}{\end{altenv}}
  \newenvironment{invisibleenv}{\begin{altenv}{\begingroup\setbox0=\vbox\bgroup}{\egroup\endgroup}{}{}}{\end{altenv}}

  \AtBeginDocument{
    % These have to be done late...
    \renewcommand<>{\color}{\alt#1{\beameroriginal{\color}}{\beamer@gobbleoptionalinsp}}
  }
}

\mode
<all>
{
  \renewcommand<>{\textbf}{\only#1{\beameroriginal{\textbf}}}
  \renewcommand<>{\textit}{\only#1{\beameroriginal{\textit}}}
  \renewcommand<>{\textmd}{\only#1{\beameroriginal{\textmd}}}
  \renewcommand<>{\textnormal}{\only#1{\beameroriginal{\textnormal}}}
  \renewcommand<>{\textrm}{\only#1{\beameroriginal{\textrm}}}
  \renewcommand<>{\textsc}{\only#1{\beameroriginal{\textsc}}}
  \renewcommand<>{\textsf}{\only#1{\beameroriginal{\textsf}}}
  \renewcommand<>{\textsl}{\only#1{\beameroriginal{\textsl}}}
  \renewcommand<>{\texttt}{\only#1{\beameroriginal{\texttt}}}
  \renewcommand<>{\textup}{\only#1{\beameroriginal{\textup}}}
}

\renewcommand<>{\hypertarget}[2]{\only#3{\beameroriginal{\hypertarget}{#1}{#2}}}
\renewcommand<>{\hyperlink}[2]{\only#3{\beameroriginal{\hyperlink}{#1}{#2}}}

\mode
<article>
{
  \g@addto@macro\beamer@lastminutepatches{%
    % These have to be done late...
    \renewcommand<>{\hypertarget}[2]{\only#3{\beameroriginal{\hypertarget}{#1}{#2}}}%
    \renewcommand<>{\hyperlink}[2]{\only#3{\beameroriginal{\hyperlink}{#1}{#2}}}
  }
}

\newcommand<>{\emph}[1]{{\only#2{\itshape}#1}}

\mode
<article>
{
  \renewcommand<>{\emph}{\only#1{\beameroriginal\emph}}
}

%
% Insert area
%
\newenvironment{overlayarea}[2]{%
  \beamer@animht=#1\relax
  \beamer@animwd=#2\relax
  \setbox\beamer@areabox=\vbox to\beamer@animwd\bgroup
  \strut\begin{minipage}[t]{\beamer@animht}%
  % Make the minipage behave like the main part of the slide
  \normalfont
  \raggedright
  }
  {%
  \end{minipage}\endgraf\vfil
  \egroup
  \wd\beamer@areabox=\beamer@animht
  \ht\beamer@areabox=\beamer@animwd
  \dp\beamer@areabox=0pt %
  \box\beamer@areabox
}

\mode
<article>
{
  \newenvironment{overlayarea}[2]{}{}% no effect
}

\newenvironment{overprint}[1][\textwidth]{%
  \let\beamer@noargsonslide=\beamer@onslide
  \let\beamer@noargsvisibleonslide=\beamer@onslide
  \beamer@animht=0pt\relax
  \beamer@animdp=0pt\relax
  \beamer@animwd=#1\relax
  \setbox\beamer@animcurrent=\box\beamer@animshowbox % makes animshowbox empty
  \setbox\beamer@animcurrent=\hbox{}%
  \def\beamer@endaction{}%
}{%
  \beamer@endaction
  \ifvoid\beamer@animshowbox\setbox\beamer@animshowbox=\hbox{}\fi
  \ht\beamer@animshowbox=\beamer@animht
  \dp\beamer@animshowbox=\beamer@animdp
  \wd\beamer@animshowbox=\beamer@animwd
  \box\beamer@animshowbox
}

\def\beamer@onslide<#1>{%
  \beamer@endaction
  \alt<#1>{%
    \ifvoid\beamer@animshowbox\else\ClassError{beamer}{Overprints may not overlap}{}\fi
    \def\beamer@endaction{%
    \end{minipage}%
    \egroup
      \ifdim\beamer@animht<\ht\beamer@animcurrent\beamer@animht=\ht\beamer@animcurrent\fi
      \ifdim\beamer@animdp<\dp\beamer@animcurrent\beamer@animdp=\dp\beamer@animcurrent\fi
      \setbox\beamer@animshowbox=\copy\beamer@animcurrent}%
  }%
  {\def\beamer@endaction{%
    \end{minipage}%
    \egroup
      \ifdim\beamer@animht<\ht\beamer@animcurrent\beamer@animht=\ht\beamer@animcurrent\fi
      \ifdim\beamer@animdp<\dp\beamer@animcurrent\beamer@animdp=\dp\beamer@animcurrent\fi}%
  }%
  \setbox\beamer@animcurrent=\vbox\bgroup\begin{minipage}[t]{\beamer@animwd}%
}

\mode
<article>
{
  \newenvironment{overprint}[1][]%
  {%
    \let\beamer@noargsonslide=\beamer@onslide%
    \let\beamer@noargsvisibleonslide=\beamer@onslide%
    \begin{onlyenv}}
    {\end{onlyenv}}

  \def\beamer@onslide<#1>{\end{onlyenv}\begin{onlyenv}<#1>}
}


%
% Transitions - DISTRACTING and to be used with CARE!!!!
%

\define@key{beamertrans}{direction}{\def\beamer@transdir{ /Di #1 }}
\define@key{beamertrans}{duration}{\def\beamer@dur{ /D #1 }}

\newcommand\beamer@dotrans[2][]{%
  \def\beamer@transdir{}%
  \def\beamer@dur{}%
  \setkeys{beamertrans}{#1}%
  \edef\beamer@temp{{pdfpagetransition={#2 \beamer@transdir\space\beamer@dur}}}%
  \expandafter\hypersetup\beamer@temp}


\mode
<article>
{
  \newcommand\beamer@dotrans[2][]{}
}

\mode
<all>
{
  \newcommand<>{\transblindshorizontal}[1][]{\only#2{\beamer@dotrans[{#1}]{Blinds /Dm /H}}}
  \newcommand<>{\transblindsvertical}[1][]{\only#2{\beamer@dotrans[{#1}]{Blinds /Dm /V}}}
  \newcommand<>{\transboxin}[1][]{\only#2{\beamer@dotrans[{#1}]{Box /M /I}}}
  \newcommand<>{\transboxout}[1][]{\only#2{\beamer@dotrans[{#1}]{Box /M /O}}}
  \newcommand<>{\transcover}[1][]{\only#2{\beamer@dotrans[{#1}]{Cover}}}
  \newcommand<>{\transdissolve}[1][]{\only#2{\beamer@dotrans[{#1}]{Dissolve}}}
  \newcommand<>{\transfade}[1][]{\only#2{\beamer@dotrans[{#1}]{Fade}}}
  \newcommand<>{\transglitter}[1][]{\only#2{\beamer@dotrans[{#1}]{Glitter}}}
  \newcommand<>{\transpush}[1][]{\only#2{\beamer@dotrans[{#1}]{Push}}}
  \newcommand<>{\transreplace}[1][]{\only#2{\beamer@dotrans[{#1}]{R}}}
  \newcommand<>{\transsplitverticalin}[1][]{\only#2{\beamer@dotrans[{#1}]{Split /Dm /V /M /I}}}
  \newcommand<>{\transsplitverticalout}[1][]{\only#2{\beamer@dotrans[{#1}]{Split /Dm /V /M /O}}}
  \newcommand<>{\transsplithorizontalin}[1][]{\only#2{\beamer@dotrans[{#1}]{Split /Dm /H /M /I}}}
  \newcommand<>{\transsplithorizontalout}[1][]{\only#2{\beamer@dotrans[{#1}]{Split /Dm /H /M /O}}}
  \newcommand<>{\transuncover}[1][]{\only#2{\beamer@dotrans[{#1}]{Uncover}}}
  \newcommand<>{\transwipe}[1][]{\only#2{\beamer@dotrans[{#1}]{Wipe}}}
  \newcommand<>{\transfly}[1][]{\only#2{\beamer@dotrans[{#1}]{Fly}}}
}

\newcommand<>{\transduration}[1]{\only#2{\hypersetup{pdfpageduration={#1}}}}

\@ifundefined{IfPDFManagementActiveTF}{}
 {
  \IfPDFManagementActiveTF
   {
     \renewcommand\beamer@dotrans[2][]{%
           \hypersetup{pdfpagetransition={style=#2,#1}}%
     }
     \renewcommand<>{\transblindshorizontal}[1][]{\only#2{\beamer@dotrans[{#1,direction=H}]{Blinds}}}
     \renewcommand<>{\transblindsvertical}[1][]{\only#2{\beamer@dotrans[{#1,direction=V}]{Blinds}}}
     \renewcommand<>{\transboxin}[1][]{\only#2{\beamer@dotrans[{#1,motion=I}]{Box}}}
     \renewcommand<>{\transboxout}[1][]{\only#2{\beamer@dotrans[{#1,motion=O}]{Box}}}
     \renewcommand<>{\transsplitverticalin}[1][]{\only#2{\beamer@dotrans[{#1,direction=V,motion=I}]{Split}}}
     \renewcommand<>{\transsplitverticalout}[1][]{\only#2{\beamer@dotrans[{#1,direction=V,motion=O}]{Split}}}
     \renewcommand<>{\transsplithorizontalin}[1][]{\only#2{\beamer@dotrans[{#1,direction=H,motion=I}]{Split}}}
     \renewcommand<>{\transsplithorizontalout}[1][]{\only#2{\beamer@dotrans[{#1,direction=H,motion=O}]{Split}}}
     \renewcommand<>{\transreplace}[1][]{\only#2{\beamer@dotrans[{#1}]{R}}}
   }
   {}
 }


\mode
<article>
{
  \newcommand<>{\transduration}[1]{}
}


%
% Animation macros
%

\protected\def\animate<#1>{\transduration<#1| handout:0| trans:0>{0}}
\protected\def\animatevalue<#1-#2>#3#4#5{%
  \only<#1-#2>{}% enforce these frames
  \ifnum\beamer@slideinframe<#1\relax
    #3=#4\relax
  \else
    \ifnum\beamer@slideinframe<#2\relax
      #3=#4\relax
      #3=-#3\relax
      \advance#3 by#5\relax
      \beamer@animationtempa=#2\relax
      \advance\beamer@animationtempa by-#1 %
      \ifnum\beamer@animationtempa>0 %
        \divide#3 by\beamer@animationtempa
      \fi
      \beamer@animationtempa=\beamer@slideinframe
      \advance\beamer@animationtempa by-#1 %
      \multiply#3 by\beamer@animationtempa
      \advance#3 by#4 %
    \else
      #3=#5\relax
    \fi
  \fi
}

\mode
<article>
{
  \protected\def\animate<#1>{}
  \protected\def\animatevalue<#1-#2>#3#4#5{#3=#4\relax}
}

%
% Labels in overlays
%
\g@addto@macro\beamer@lastminutepatches{%
  \let\beamer@origlabel\label
  \def\label{\@ifnextchar<{\beamer@label}{\beamer@label<1>}}

  \let\beamer@origlabel@in@display\label@in@display
  \def\label@in@display{\@ifnextchar<{\beamer@label@in@display}{\beamer@label@in@display<1>}}
}

\def\beamer@label<#1>#2{\alt<#1>{\beamer@origlabel{#2}\beamer@nameslide{#2}}{\beamer@dummynameslide}}
\def\beamer@label@in@display<#1>#2{\alt<#1>{\beamer@origlabel@in@display{#2}\beamer@nameslide{#2}}{\beamer@dummynameslide}}

\mode
<article>
{
  \g@addto@macro\beamer@lastminutepatches{
    % Has to be done late...
    \renewcommand<>{\label}{\alt#1{\beameroriginal{\label}}{\@gobble}}
    \renewcommand<>{\label@in@display}{\alt#1{\beameroriginal{\label@in@display}}{\@gobble}}
  }
}

\AtBeginDocument
{
  \let\beamer@origref=\ref
  \def\ref{%
    \@ifstar
      {\beamer@origref}
      {\beamer@ref}%
  }
  \long\def\beamer@ref#1{%
    \hyperlink{#1}{\beamer@origref{#1}}%
  }
}

\mode
<all>
